package nl.boektrust.btsoep.client;

import nl.boektrust.btsoep.shared.model.AvailabilityCode;
import nl.boektrust.btsoep.shared.model.LanguageCode;
import nl.boektrust.btsoep.shared.model.ProductFormCode;
import nl.boektrust.btsoep.shared.model.EditionTypeCode;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class EditBookVerticalPanelSimpleListbox extends EditBookVerticalPanel implements ValueChangeHandler<String>, BlurHandler, ChangeHandler {
	private ListBox theListBox = new ListBox();
	EditBookDialog parentDialog;
	enum ListBoxType {
		ProductFormCode, EditionTypeCode, LanguageCode, Publisher, Druk, AvailabilityCode, Unknown;
	}
	ListBoxType listBoxType = ListBoxType.Unknown; // has an object from one of the enums that this listbox can show. 

	
	public EditBookVerticalPanelSimpleListbox(String label, EditBookDialog parentDialog) {
		super(label);
		theListBox.addChangeHandler(this);
		theListBox.addBlurHandler(this);
		theListBox.setFocus(true);
		editPanel.add(theListBox);
		this.parentDialog = parentDialog;
		setFieldLabel(label);

		setToDisplayMode();
	}
	
	public EditBookVerticalPanelSimpleListbox() {
		this("", null);
	}
	
	@Override 
	void setToDisplayMode() {
		super.setToDisplayMode();
		theLabel.setText(getValue());
	}

	
	public String getValue() {
		String aString = new String("");
		if (theListBox != null && theListBox.getItemCount() > 0) {
			try {
				aString = theListBox.getValue(theListBox.getSelectedIndex());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return aString;
	}
	
	
	void removeListboxItems() {
		for (int i = theListBox.getItemCount()-1; i>=0; i--) theListBox.removeItem(i);
	}
	
	public void populateWithLanguages(LanguageCode l) {
		this.listBoxType = ListBoxType.LanguageCode;
		try {
			removeListboxItems();
			for (LanguageCode c : LanguageCode.values()) {
				theListBox.addItem(LanguageCode.stringValue(c));
				if (c == l) {
					theListBox.setSelectedIndex(theListBox.getItemCount()-1);
					valueChanged();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setToDisplayMode();
	}
	
	public void valueHasChanged(LanguageCode languageCode) {	}


	public void populateWithProductForm(ProductFormCode arg) {
		this.listBoxType = ListBoxType.ProductFormCode;

		try {
			removeListboxItems();
			for (ProductFormCode c : ProductFormCode.values())
			{
				theListBox.addItem(ProductFormCode.stringValue(c));
				if (c == arg) {
					theListBox.setSelectedIndex(theListBox.getItemCount()-1);
					valueChanged();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setToDisplayMode();

	}
	
	public void valueHasChanged(ProductFormCode productFormCode) {	}
	
	public void populateWithEdition(EditionTypeCode arg) {
		this.listBoxType = ListBoxType.EditionTypeCode;

		try {
			removeListboxItems();
			for (EditionTypeCode c : EditionTypeCode.values()) {
				theListBox.addItem(EditionTypeCode.stringValue(c));
				if (c == arg) {
					theListBox.setSelectedIndex(theListBox.getItemCount()-1);
					valueChanged();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setToDisplayMode();

	}

	public void valueHasChanged(EditionTypeCode editionTypeCode) {	}

	
	public void populateWithPublishers() {
		this.listBoxType = ListBoxType.Publisher;

		try {
			removeListboxItems();
			theListBox.addItem("Uitgeverij Heureka");
			theListBox.addItem("Meulenhoff");
			theListBox.addItem("Cambridge University Press");
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setToDisplayMode();

	}

	public void populateWithDruk(int druk) {
		this.listBoxType = ListBoxType.Druk;

		try {
			removeListboxItems();
			for (int i=1; i<200; i++) {
				theListBox.addItem(new String(i+"e druk"), new Integer(i).toString());
			}
			if (druk >= 1) theListBox.setSelectedIndex(druk-1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setToDisplayMode();

	}
	public void valueHasChanged(int druk) {	}

	
	public void populateWithAvailability(AvailabilityCode arg) {
		this.listBoxType = ListBoxType.AvailabilityCode;

		try {
			removeListboxItems();
			for (AvailabilityCode c : AvailabilityCode.values()) {
				theListBox.addItem(AvailabilityCode.stringValue(c));
				if (c == arg) {
					theListBox.setSelectedIndex(theListBox.getItemCount()-1);
					valueChanged();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setToDisplayMode();

	}
	public void valueHasChanged(AvailabilityCode availabilityCode) {	}



	

	public void onValueChange(ValueChangeEvent<String> event) {
		value = theListBox.getItemText(theListBox.getSelectedIndex());
		theLabel.setText((String)value);
		//	.getValue();
		
	}

	/**
	 * @return the theLabel
	 */
	public Label getTheLabel() {
		return theLabel;
	}

	/**
	 * @param theLabel the theLabel to set
	 */
	public void setTheLabel(Label theLabel) {
		this.theLabel = theLabel;
	}

	/**
	 * @return the theListBox
	 */
	public ListBox getTheListBox() {
		return theListBox;
	}

	/**
	 * @param theListBox the theListBox to set
	 */
	public void setTheListBox(ListBox theListBox) {
		this.theListBox = theListBox;
	}

	public void valueChanged() {
		value = theListBox.getItemText(theListBox.getSelectedIndex());
		this.setToDisplayMode();
		switch (listBoxType) {
		case AvailabilityCode : 
			valueHasChanged(AvailabilityCode.parseStringIntoAvailabilityCode((String)value));
			System.out.println("LISTBOX: Value changed, listbox is type availabilitycode");
			break;
		case ProductFormCode: 
			valueHasChanged( ProductFormCode.parseStringIntoProductFormCode((String)value));
			System.out.println("LISTBOX: Value changed, listbox is type ProductFormCode");
			break;
		case EditionTypeCode:
			valueHasChanged(EditionTypeCode.parseStringIntoEditionTypeCode((String)value));
			System.out.println("LISTBOX: Value changed, listbox is type EditionTypeCode");
			break;
		case Publisher:
			break;
		case Druk:
			valueHasChanged(theListBox.getSelectedIndex()+1);
			System.out.println("LISTBOX: Value changed, listbox is type Druk");
			break;
		case LanguageCode:
			valueHasChanged(LanguageCode.parseStringIntoLanguageCode((String)value));
			System.out.println("LISTBOX: Value changed, listbox is type LanguageCode");
			break;
		}
	}
	
	public void onChange(ChangeEvent event) {
		valueChanged();
		if (parentDialog != null) parentDialog.panelHasChanged();
	}

	public void onBlur(BlurEvent event) {
		if ((Widget) event.getSource() == (Widget)theListBox) {
			setInEditMode(false);
			this.setToDisplayMode();
		}
		
	}

}
